<template>
  <div class="user-list">

    <div class="header">
      <h2>用户列表</h2>
      <el-button type="primary" icon="Plus" @click="addUser">新增用户</el-button>
    </div>
    <div class="table">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column type="selection" />
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="用户名" align="center" />
        <el-table-column prop="userName" label="用户名" align="center" />
        <el-table-column prop="realName" label="真实姓名" align="center" />
        <el-table-column prop="cellPhone" label="手机号码" align="center" />
        <el-table-column prop="date" label="状态" align="center">
          <!-- 作用域插槽 -->
          <template #default="scope">
            <el-tag v-if="scope.row.mg_state" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createAt" label="创建时间" align="center">
          <template #default="scope">
            {{ formatUTC(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column prop="updateAt" label="更新时间" align="center">
          <template #default="scope">
            {{ formatUTC(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <!-- 删除 -->
          <template #default="scope">
            <el-button size="small" type="primary" text icon="Edit">编辑</el-button>
            <el-button size="small" type="danger" text icon="Delete" @click="console.log(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30]"
        size="small" layout="total, sizes, prev, pager, next, jumper" :total="userListTotal"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import useSystemStore from '@/store/main/system';
import { formatUTC } from '@/utils/formatDate';
import { storeToRefs } from 'pinia';
import { ref } from 'vue';

const emit = defineEmits(['addUser'])

// 页码相关

const currentPage = ref(1)
const pageSize = ref(10)

// 1. 发起action 请求userlist数据
const systemStore = useSystemStore()
fetchListData()

// 2. 通过storeToRefs拿到响应式数据 渲染数据
const { userList, userListTotal } = storeToRefs(systemStore)
// const usersData = systemStore.userList   // 此种方式拿到的数据不是响应式的


const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
  fetchListData()
}
const handleCurrentChange = (val: number) => {
  console.log('val: ', val);
  fetchListData()
}

// 分页变化重新发起网络请求
function fetchListData(formData: any = {}) {
  const size = pageSize.value
  const offset = (currentPage.value - 1) * pageSize.value
  systemStore.postUserListAction({ size, offset, ...formData })

}

function addUser() {
  emit('addUser')
}

// 暴露查询方法
defineExpose({
  fetchListData
})
</script>
<style lang="less" scoped>
.user-list {
  background-color: white;
  margin-top: 20px;
  padding: 20px;

}

.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

// 覆盖el-table的样式
.el-table {
  :deep(.el-table__cell) {
    padding: 10px 0;
  }

  .el-button {
    margin-left: 0px;
    padding: 0px 6px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>